package cn.fl.custpay.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.custpay.condition.CmsCmmCntDcQueryCondition;
import cn.fl.custpay.constant.CmsCmmCntDcConstant;
import cn.fl.custpay.service.ICmsCmmCntDcService;
import cn.fl.custpay.vo.CmsCmmCntDcVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;

/**
 * @Description: 通用内容定义表 控制层
 */
@Controller
@RequestMapping("/custpay/cmsCmmCntDc")
public class CmsCmmCntDcController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsCmmCntDcController.class);

	/**
	 * CmsCmmCntDc 服务层BEAN
	 */
	@Autowired
	private ICmsCmmCntDcService cmsCmmCntDcService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toCmsCmmCntDcList.do", method = { RequestMethod.GET})
	public ModelAndView toCmsCmmCntDcList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("custpay/cmsCmmCntDc/cmsCmmCntDcList");
		List<KeyValueVO> cntcdlist = this.cmsCmmCntDcService.getKvList(CmsCmmCntDcConstant.CODE_GRP_CNT_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		view.addObject("cntcdlist",cntcdlist);
		BaseLogUtils.info(logger, "toCmsCmmCntDcList", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/modifyCmsCmmCntDcPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView modifyCmsCmmCntDcPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("custpay/cmsCmmCntDc/modifyCmsCmmCntDc");
		CmsCmmCntDcVO entity = new CmsCmmCntDcVO();
		if(StringUtils.isNotEmpty(id)){
			entity = this.cmsCmmCntDcService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> cntcdlist = this.cmsCmmCntDcService.getKvList(CmsCmmCntDcConstant.CODE_GRP_CNT_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		view.addObject("cntcdlist",cntcdlist);
		view.addObject("cmscmmcntdc",entity);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initCmsCmmCntDcList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initCmsCmmCntDcList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String cntCd = request.getParameter("cntCd");
		String cntNm = request.getParameter("cntNm");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		CmsCmmCntDcQueryCondition qc = new CmsCmmCntDcQueryCondition();
		if (StringUtils.isNotEmpty(cntCd)) {
			qc.setCntCd(Long.valueOf(cntCd));
		}
		if (StringUtils.isNotEmpty(cntNm)) {
			qc.setCntNm(cntNm);
		}
		page.setCondition(qc);
		
		//TODO SOMETHING
		Pager pager = this.cmsCmmCntDcService.searchListPage(page);
		List<CmsCmmCntDcVO> list = (List<CmsCmmCntDcVO>)pager.getResultList();
		for(CmsCmmCntDcVO _vo : list){
			if(null != _vo.getCntCd()){
				Map<String,String> cntcdmap = this.cmsCmmCntDcService.searchGroupToMap(CurrentThreadContext.getFlServiceSysCd(),CurrentThreadContext.getCurrentOrgCd(),CmsCmmCntDcConstant.CODE_GRP_CNT_CD);
				_vo.setCntCdNm(cntcdmap.get(String.valueOf(_vo.getCntCd())));
			}
			if(null != _vo.getIsOl()){
				Map<String,String> isolmap = new HashMap<String,String>();
				isolmap.put("0","未上线");
				isolmap.put("1","上线");
				_vo.setIsOlNm(isolmap.get(String.valueOf(_vo.getIsOl())));
			}
		}
		ListPageVO<CmsCmmCntDcVO> listPage = new ListPageVO<CmsCmmCntDcVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initCmsCmmCntDcList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.cmsCmmCntDcService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CmsCmmCntDcVO vo = this.cmsCmmCntDcService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		CmsCmmCntDcVO entity = null;
		try {
			entity = new CmsCmmCntDcVO();
			//TODO ADD PARAM
			Long id = this.cmsCmmCntDcService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsCmmCntDcVO entity = null;
		try {
			entity = new CmsCmmCntDcVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.cmsCmmCntDcService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyCmsCmmCntDc.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyCmsCmmCntDc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String cntId = request.getParameter("cntId");
		String cntCd = request.getParameter("cntCd");
		String cntNm = request.getParameter("cntNm");
		String isOl = request.getParameter("isOl");
		String cntDtl = request.getParameter("cntDtl");
		DataResultVO result = null; //new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		CmsCmmCntDcVO entity = null;
		try {
			entity = new CmsCmmCntDcVO();
			if(StringUtils.isNotEmpty(cntCd)){
				entity.setCntCd(Long.valueOf(cntCd));
			}
			if(StringUtils.isNotEmpty(cntNm)){
				entity.setCntNm(cntNm);
			}
			if(StringUtils.isNotEmpty(isOl)){
				entity.setIsOl(Integer.valueOf(isOl));
			}
			if(StringUtils.isNotEmpty(cntDtl)){
				entity.setCntDtl(cntDtl);
			}
			if (StringUtils.isNotEmpty(cntId)){
				result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
				entity.setCntId(Long.valueOf(cntId));
				entity.setMdfTm(new Date());
				entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				boolean success = this.cmsCmmCntDcService.modify(entity);
				if(!success){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
					BaseLogUtils.info(logger, "modifyCmsCmmCntDc", "修改失败.[id="+cntId+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
				}
			}else {
				result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
				entity.setCrtTm(new Date());
				entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				Long id = this.cmsCmmCntDcService.add(entity);
				if(null == id){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_ADD_FAIL);
					BaseLogUtils.info(logger, "modifyCmsCmmCntDc", "新增失败.[id="+cntId+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
				}
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyCmsCmmCntDc", "修改失败.[id="+cntId+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyCmsCmmCntDc", "修改失败.[id="+cntId+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 上线下线
	 */
	@RequestMapping(value = "/isOlCmsCmmCntDc.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object isOlCmsCmmCntDc(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String isol = request.getParameter("isol");
		String cntCd = request.getParameter("cntCd");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id) || StringUtils.isEmpty(isol) || StringUtils.isEmpty(cntCd)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "isOlCmsBnrDc", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsCmmCntDcVO entity = null;
		try {
			entity = new CmsCmmCntDcVO();
			if(StringUtils.isNotEmpty(id)){
				entity.setCntId(Long.valueOf(id));
			}
			if(StringUtils.isNotEmpty(cntCd)){
				entity.setCntCd(Long.valueOf(cntCd));
			}
			if(StringUtils.isNotEmpty(isol)){
				if("0".equals(isol)){
					entity.setIsOl(0);
				}else if("1".equals(isol)){
					entity.setIsOl(1);
				}
			}
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			boolean success = this.cmsCmmCntDcService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
				BaseLogUtils.info(logger, "isOlCmsCmmCntDc", "修改数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "isOlCmsCmmCntDc", "修改数据成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "isOlCmsCmmCntDc", "修改数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "isOlCmsCmmCntDc", "修改数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

